<template>
  <div v-if="proxy.isAuth(['ROOT', 'RULE:SELECT'])">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="form">
      <el-form-item prop="name">
        <el-input v-model="dataForm.name" placeholder="规则名称" maxlength="20" class="input" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchHandle()" type="primary">
          查询
        </el-button>
        <el-button type="primary" :disabled="!proxy.isAuth(['ROOT', 'RULE:INSERT'])" @click="addHandle()">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <!--分页表格-->

    <el-table v-loading="data.loading" :data="data.dataList" border :header-cell-style="{'background':'#f5f7fa'}">
      <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
        <template #default="scope">
          <span>{{ (data.pageIndex - 1) * data.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" header-align="left" align="left" label="规则名称" min-width="150"/>
      <el-table-column prop="remark" header-align="left" align="left" label="备注消息" min-width="350"/>
      <el-table-column prop="count" header-align="center" align="center" label="关联套餐数量" min-width="80"/>
      <el-table-column header-align="center" align="center" label="操作" min-width="150">
        <template #default="scope">
          <el-button type="text" v-if="proxy.isAuth(['ROOT', 'RULE:UPDATE'])" @click="updateHandle(scope.row.id)">
            修改
          </el-button>
          <el-button type="text" :disabled="scope.row.count > 0" v-if="proxy.isAuth(['ROOT', 'RULE:DELETE'])"
                     @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="data.pageIndex"
        :page-sizes="[10, 20, 30]"
        :page-size="data.pageSize"
        :total="data.totalCount"
        style="margin-top: 20px;"
        layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
  <el-dialog :title="!dialog.dataForm.id ? '新增' : '修改'" v-if="proxy.isAuth(['ROOT', 'RULE:INSERT', 'RULE:UPDATE'])"
             close-on-click-modal="false" v-model="dialog.visible" custom-class="dialog" width="500px">
    <el-form :model="dialog.dataForm" ref="dialogForm" :rules="dialog.dataRule" label-width="80px">
      <el-form-item label="规则名称" prop="name">
        <el-input v-model="dialog.dataForm.name" maxlength="20" clearable/>
      </el-form-item>
      <el-form-item label="规则内容" prop="rule">
        <el-input v-model="dialog.dataForm.rule" type="textarea" rows="5" clearable/>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model="dialog.dataForm.remark" type="textarea" rows="3" clearable/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSumbit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ref, reactive, getCurrentInstance, onMounted} from 'vue';
import {Delete} from "@element-plus/icons-vue";
import router from "../../router/index.ts";

const {proxy} = getCurrentInstance();

const dataForm = reactive({
  name: null
});

const dataRule = reactive({
  name: [
    {required: false, message: '规则名称格式错误', pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$'}
  ]
});

const data = reactive({
  dataList: [],
  pageIndex: 1,
  pageSize: 10,
  totalCount: 0,
  loading: false
});

const dialog = reactive({
  visible: false,
  dataForm: {
    id: null,
    name: null,
    rule: null,
    remark: null
  },
  dataRule: {
    name: [
      {
        required: true, message: '规则名称不能为空'
      },
      {
        pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$',
        message: '规则名称格式错误'
      }
    ],
    rule: [
      {
        required: true,
        trigger: 'blur',
        message: '规则内容不能为空'
      }
    ]
  }
});

// 加载促销规则分页数据
function loadDataList() {
  data.loading = true;
  let json = {
    page: data.pageIndex,
    length: data.pageSize,
    name: dataForm.name
  }
  proxy.$http('/mis/rule/searchByPage', 'POST', json, true, resp => {
    let page = resp.page;
    let list = page.list;
    data.dataList = list;
    data.totalCount = page.totalCount;
    data.loading = false;
  })
}

loadDataList();

// 查询按钮回调函数
function searchHandle() {
  proxy.$refs['form'].validate(valid => {
    if (valid) {
      proxy.$refs['form'].clearValidate();
      if (dataForm.name == null) {
        dataForm.name = null;
      }
      if (data.pageIndex != 1) {
        data.pageIndex = 1;
      }
      loadDataList();
    } else {
      return false;
    }
  });
}

// 修改按钮回调函数
function updateHandle(id) {
  dialog.dataForm.id = id;
  dialog.visible = true;
  proxy.$nextTick(() => {

    proxy.$refs['dialogForm'].resetFields();
    let json = {
      id: id
    };
    proxy.$http('/mis/rule/searchById', 'POST', json, true, resp => {
      let result = resp.result;
      dialog.dataForm.name = result.name;
      dialog.dataForm.rule = result.rule;
      dialog.dataForm.remark = result.remark;
    });
  });
}

// 删除按钮回调函数
function deleteHandle(id) {
  proxy.$confirm(`确定要删除选中的促销规则吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    let json = {id: id};
    proxy.$http('/mis/rule/deleteById', 'POST', json, true, resp => {
      if (resp.rows > 0) {
        proxy.$message({
          message: '删除成功',
          type: 'success',
          duration: 1200,
          onClose: () => {
            loadDataList();
          }
        });
      } else {
        proxy.$message({
          message: '删除失败',
          type: 'error',
          duration: 1200
        });
      }
    });
  });
}

// 分页控件相关函数
function sizeChangeHandle(val) {
  data.pageIndex = 1;
  data.pageSize = val;
  loadDataList();
}

// 分页控件相关函数
function currentChangeHandle(val) {
  data.pageIndex = val;
  loadDataList();
}

// 新增按钮回调函数
function addHandle() {
  dialog.dataForm.id = null;
  dialog.dataForm.name = null;
  dialog.dataForm.rule = null;
  dialog.dataForm.remark = null;
  dialog.visible = true;
  proxy.$nextTick(() => {
    proxy.$refs['dataForm'].resetFields();
  });
}

// 弹窗控件确定按钮(提交表单)
function dataFormSumbit() {
  proxy.$refs['dialogForm'].validate(valid => {
    if (valid) {
      proxy.$http(`/mis/rule/${dialog.dataForm.id == null ? 'insert' : 'update'}`, 'POST', dialog.dataForm, true, resp => {
        if (resp.rows == 1) {
          proxy.$message({
            message: '操作成功',
            type: 'success',
            duration: 1200,
            onClose: () => {
              dialog.visible = false;
              loadDataList();
            }
          })
        } else {
          proxy.$message({
            message: '操作失败',
            type: 'error',
            duration: 1200
          });
        }
      })
    }
  })
}
</script>

<style scoped lang="less">
@import url('rule.less');
</style>